package com.hhkj.admin.bank.controller;

import com.hhkj.admin.bank.common.valid.*;
import com.hhkj.admin.bank.domain.Bank;
import com.hhkj.admin.bank.model.req.BankReq;
import com.hhkj.admin.bank.model.res.BankRes;
import com.hhkj.admin.bank.service.biz.BankBizService;
import com.hhkj.admin.question.model.extend.QuestionExportModel;
import com.hhkj.admin.question.model.extend.QuestionImportModel;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.LogTitlePrefix;
import com.ruoyi.common.annotation.PermissionPrefix;
import com.ruoyi.common.base.biz.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.excl.utils.ExcelUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 题库信息Controller
 *
 * @author ruoyi
 * @since 2023-05-16
 */
@LogTitlePrefix("题库信息")
@PermissionPrefix("train:bank")
@RestController
@RequestMapping("/bank")
public class BankController extends BaseController<Bank, BankReq, BankRes, BankBizService>
{

    /** 发布题库信息 */
    @PostMapping("/publish")
    public AjaxResult publish(@Validated(VPublishBank.class) @RequestBody BankReq req)
    {
        return AjaxResult.success(this.getBizService().publish(req));
    }

    /** 选题规则 */
    @PostMapping("/rule/all/course/bank/list")
    public TableDataInfo listAllCoursePublishOfRule(@Validated(VListAllCoursePublishOfRule.class) @RequestBody BankReq req)
    {
        return getDataTable(this.getBizService().listAllCoursePublishOfRule(req));
    }

    /** 上下架 */
    @PreAuthorize("@ss.hasPermi(this, 'publish')")
    @Log(title = "上下架", businessType = BusinessType.UPDATE)
    @PutMapping("/publish/flag")
    public AjaxResult updatePublishFlag(@Validated(VEditPublishFlag.class) @RequestBody BankReq req)
    {
        return toAjax(this.getBizService().updatePublishFlag(req.getId(), req.getPublishFlag()));
    }

    @Override
    public AjaxResult save(BankReq bankReq)
    {
        return this.getBizService().save(bankReq) ? AjaxResult.success() : AjaxResult.error();
    }

    /** 修改题库名称 */
    @PreAuthorize("@ss.hasPermi(this, 'edit')")
    @Log(title = "修改名称", businessType = BusinessType.UPDATE)
    @PutMapping("/name")
    public AjaxResult updateName(@Validated(VEditName.class) @RequestBody BankReq req)
    {
        return toAjax(this.getBizService().updateName(req.getId(), req.getName()));
    }

    /** 题库题目导入模板下载 */
    @PreAuthorize("@ss.hasPermi(this, 'add')")
    @PostMapping("/question/import/template/download")
    public void downloadQuestionImportTemplate(@Validated(VDownloadQuestionImportTemplate.class) @RequestBody BankReq req, HttpServletResponse response)
    {
        ExcelUtils.downLoadExcel(QuestionImportModel.FILE_NAME, response, this.getBizService().createBankQuestionImportTemplate(req));
    }

    @PostMapping("/question/export")
    public void exportQuestion(@RequestBody BankReq req, HttpServletResponse response)
    {
        ExcelUtils.exportMultiSheet(QuestionImportModel.FILE_NAME, response, this.getBizService().createExportQuestion(req));
    }

}
